#region Imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Agreements
{
	#region Class ProjectHeader
	/// <summary>
	/// Provides a cacheable data storage for the labels used in the ProjectHeaderBar.
	/// </summary>
	public class ProjectHeader : BO.BusinessObject
	{
		#region Constants
		private static string ProjectHeaderKey = GenerateKey.MakeKey("ProjectHeader_39DCF35A-7805-45da-AA8A-82B3CC34526F",GenerateKey.Agreement);
		#endregion

		#region Instance Variables
		private int resourceId = -1;
		private int budgetYear = 0;
		#endregion

		#region Public Properties
		/// <summary>
		/// Get the key to be used in all caching operations for this instance of the object.
		/// </summary>
		public static string CacheKey	{ get { return ProjectHeaderKey; } }

		/// <summary>
		/// Gets the currently stored FundNumber in this ProjectHeader. If none exists, returns null.
		/// </summary>
		public string FundNumber
		{
			get
			{
				return (DataContainer.Tables.Count == 0 || DataContainer.Tables[0].Rows.Count == 0) ?
						null :
						DataContainer.Tables[0].Rows[0][0] as string;
			}
		}

		/// <summary>
		/// Gets the currently stored ResourceId in this ProjectHeader. If none has been set before, returns -1.
		/// </summary>
		public int ResourceId
		{
			get { return resourceId; }
		}

		/// <summary>
		/// Gets/sets the currently stored ResourceId in this ProjectHeader.
		/// This is used by the ProjectHeaderBar.
		/// </summary>
		public int BudgetYear
		{
			get { return budgetYear; }
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Initilize the base class with the specified cache policy.
		/// </summary>
		/// <param name="cachePolicy"></param>
		public ProjectHeader(ICachePolicy cachePolicy) : base(cachePolicy)
		{
		}
		#endregion

		#region Business Object Methods
		/// <summary>
		/// Cache the current instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(ProjectHeaderKey, this);
		}
		
		/// <summary>
		/// Load the current instance with project header data for the given fund.
		/// </summary>
		/// <param name="connectionString">Database connection string</param>
		/// <param name="args">Is expected to contain a single argument - the domain user ID</param>
		public override void Load(string connectionString, params object[] args)
		{
			if (args.Length < 2 || args.Length > 3)
				throw new Exception("ProjectHeader load requires a fund number and resource ID.");

			string fundNumber = (string)args[0];
			if (fundNumber == null) fundNumber = "";
			resourceId = (int)args[1];

			SqlParameter[] parameters = DataAccess.BuildSqlParameterArray(
					"@fundNumber", SqlDbType.VarChar, fundNumber,
					"@resourceId", SqlDbType.Int, resourceId);
			if (args.Length == 3)
			{
				budgetYear = Convert.ToInt32(args[2]);
				DataAccess.AppendToSqlParameterArray(ref parameters, "@budgetYear", SqlDbType.Int, budgetYear);
			}

			DataContainer =	DataAccess.GetDataSet(connectionString, "GetProjectHeader",  parameters);
		}
		
		/// <summary>
		/// Remove the current instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(ProjectHeaderKey);
		}
		#endregion
	}
	#endregion
}
